//
//  WalletRepository.swift
//  Wallet
//
//  Created by Julian on 2022/5/7.
//

import Foundation
import RxSwift

struct WalletRepository {
    // MARK: - DATA
    private let walletSession: HttpClient
    
    // MARK: - INITIALIZE
    init(walletSession: HttpClient) {
        self.walletSession = walletSession
    }
    
    // MARK: - PUBLIC METHODS
    /// 获取钱包 coins 数据
    /// - Returns: 钱包的所有 Coin
    func getWalletCoins() -> Observable<[Coin]> {
        return walletSession
            .request(CurrencyInfo.self, from: API.currencies)
            .map { $0.currencies } // => [Coin]
            .catchAndReturn([])
    }
    
    /// 获取当前实时汇率
    /// - Returns: 汇率映射表[币种：汇率]，数据大时便于查找
    func getLiveRate() -> Observable<[String: [Tiers]]> {
        return walletSession
            .request(TierInfo.self, from: API.liveRetas)
            .map { $0.tiers.toDict { $0.cryptoCurrency } } // => { "BTC": [Tiers], "CRO": [Tiers] }
            .catchAndReturn([:])
    }
}
